package com.zhang.common.converter;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.zhang.common.enums.PurchaseStatusEnum;

/**excel导入导出时采购状态枚举转换
 * @author hongmizfb
 * @date 2024/07/04
 */
public class PurchaseStatusConverter implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 将excel转为java（导入）
     *
     * @param cellData
     * @param excelContentProperty
     * @param globalConfiguration
     * @return {@link Integer}
     */
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        String description = cellData.getStringValue();
        return PurchaseStatusEnum.getByDescription(description).getStatus();
    }

    /**导出为excel
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return {@link WriteCellData}<{@link ?}>
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(PurchaseStatusEnum.getByStatus(value).getDescription());
    }

}
